package com.tcd.outRisk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tcd.outRisk.entity.CaseReportInfo;
import com.tcd.outRisk.entity.ClaimMainInfo;
import com.tcd.outRisk.service.CaseReportInfoService;
import com.tcd.outRisk.service.ClaimMainInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报案信息表 前端控制器
 *
 * @author Jarle
 * @since 2021-05-22
 */
@RestController
@RequestMapping("/CaseReportInfo")
@CrossOrigin
public class CaseReportInfoController {
    @Autowired
    CaseReportInfoService caseReportInfo;
    @Autowired
    ClaimMainInfoService casemainInfo;
    /**
     * 方法描述
     *
     * @param
     * @description:查出所有的出险信息
     * @return: List<CaseReportInfo>
     * @author: daixingyu
     * @date: 2021-05-23  20:53
     */
    @RequestMapping("/Listall")
    public Map<String, Object> Listall(int page, int pagesize) {
        Map<String, Object> map = new HashMap<>();
        List<CaseReportInfo> list = caseReportInfo.list();
        map.put("count", list.size());
        List<CaseReportInfo> listsplice = caseReportInfo.Caselist(page, pagesize);
        map.put("list", listsplice);
        return map;
    }
    /**
     * 方法描述
     *
     * @param cases
     * @description:添加出险信息
     * @return: boolean
     * @author: daixingyu
     * @date: 2021-05-23  20:53
     */
    @RequestMapping("/Listadd")
    public boolean CaseReportInfoadd(@RequestBody CaseReportInfo cases) {
        Long id = IdWorker.getId();
        String num = 488 + "" + id.intValue();
        if (num.indexOf('-') != -1) {
            num = num.substring(0, num.lastIndexOf("-")) + num.substring(num.lastIndexOf("-") + 1, num.length() - 1);
        }
        cases.setCasereportId(Long.valueOf(num));
        return caseReportInfo.save(cases);
    }
    /**
     * 方法描述
     * 修改受理状态
     */
    @RequestMapping("/ListEditStatus")
    public int CaseReportInfoEditStatus(@RequestBody CaseReportInfo cases) {
        switch (cases.getReporttodealwith()) {
            case 1://受理
                String caseid = caseReportInfo.num(cases.getCarnumber() + "", cases.getCasereportId() + "");//得到距离保险起止7天的保单号
                int day = caseReportInfo.morenumber(cases.getCarnumber());//得到是否距离上一次出报案已超过三天
                int caseidone = 0;
                if (caseid != null)
                    caseidone = Integer.parseInt(caseid);
                if (caseidone > 0 && day > 3) {
                    Long id = IdWorker.getId();
                    String num = 479 + "" + id.intValue();
                    if (num.indexOf('-') != -1) {
                        num = num.substring(0, num.lastIndexOf("-")) + num.substring(num.lastIndexOf("-") + 1, num.length() - 1);
                    }
                    casemainInfo.save(new ClaimMainInfo(Long.valueOf(num), cases.getCasereportId(), Long.valueOf(caseid), new Date(System.currentTimeMillis()), new Date(1), 1));//添加保安信息表的信息
                    return caseReportInfo.updateById(cases) == true ? 1 : 0;
                } else if (caseidone <= 0 && day > 3) {//出险日期不在保险起止日期7天但三天内无报案信息
                    return 2;
                } else if (caseidone > 0 && day <= 3) {//出险日期在保险日期7天内但三天内有报案信息
                    return 3;
                }
                break;
            case 3://注销e
                //判断是否已经立案
                int count = casemainInfo.Clainmaincount(cases.getCasereportId() + "");
                if (count > 0) {
                    return caseReportInfo.updateById(cases) == true ? 1 : 0;
                }
                break;
        }
        return 0;
    }

    @RequestMapping("/ListEdit")
    public boolean CaseReportInfoEdit(@RequestBody CaseReportInfo cases) {
        return caseReportInfo.updateById(cases);
    }

    @RequestMapping("/Listone")
    public CaseReportInfo caseReportone(String reportId) {
        return caseReportInfo.getById(reportId);
    }

    @RequestMapping("/Listtask")
    public Map<String, Object> Caseall(int page, int pagesize) {
        List<CaseReportInfo> list = caseReportInfo.Caseall();
        Map<String, Object> map = new HashMap<>();
        map.put("count", list.size());
        Page<CaseReportInfo> page1 = PageHelper.startPage(page, pagesize);
        map.put("list", list);
        return map;
    }

    @RequestMapping("/Listupdate")
    public boolean CaseReportInfoupdatestatus(String casereportId, String reporttodealwith) {
        return caseReportInfo.CaseReportInfoupdaetstatus(casereportId, reporttodealwith);
    }
}

